<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- tabs 标签页 -->
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 表单 disabled是否禁用-->
            <el-form label-width="120px"
                     style="margin-left: 120px; margin-top:30px"
                     :model="user"
                     :rules="rules"
                     ref="form">
              <el-form-item label="手机号">
                <el-input style="width:300px"
                          disabled
                          v-model="user.mobile" />
              </el-form-item>
              <el-form-item label="姓名"
                            prop="username">
                <el-input style="width:300px"
                          v-model="user.username" />
              </el-form-item>
              <el-form-item label="密码"
                            prop="newpassword">
                <el-input style="width:300px"
                          type="password"
                          v-model="user.newpassword" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary"
                           @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo></UserInfo>
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo></JobInfo>
          </el-tab-pane>
        </el-tabs>
      </el-card>

    </div>
  </div>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { saveUserDetailById } from '@/api/employess'
import UserInfo from './components/UserInfo.vue'
import JobInfo from './components/job-info.vue'
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo, // 个人详情组件
    JobInfo   // 岗位信息
  },
  data() {
    return {
      user: {
        username: '',
        newpassword: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空' }
        ],
        newpassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度只能为6-16位' }
        ]
      }
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  // 进页面直接渲染
  created() {
    this.getUserDetail() // 数据回显
  },
  methods: {
    async getUserDetail() {
      const data = await getUserDetail(this.id)
      // 数据回显
      this.user = data
    },
    // 员工更新
    async saveUser() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      await saveUserDetailById({
        id: this.id,
        username: this.user.username,
        password: this.user.newpassword
      })
      this.$message.success('更新成功')
    }

  }

}
</script>

<style>
</style>
